package fwx.servlet;

import fwx.dto.PageDto;
import fwx.model.Barber;
import fwx.model.User;
import fwx.service.BarberService;

import fwx.service.impl.BarberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "Barber",urlPatterns = "/admin/barber")
public class BarberAdminServlet extends BaseServlet{
    private BarberService service = new BarberServiceImpl();
    private final static  int PageSize = 3;
    //管理页面
    public void manageServlet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        List<Barber> list = service.findAll();
        request.setAttribute("barberList",list);
        request.getRequestDispatcher("manage.jsp").forward(request,response);
    }
    //人员管理
    public void manageAdminServlet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        List<User> users = service.findUserAll();
        request.setAttribute("userList",users);
        request.getRequestDispatcher("usermanage.jsp").forward(request,response);
    }
    //普通页面
    public void normalServlet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        List<Barber> barbers = service.findAll();
        request.setAttribute("barberList", barbers);
        request.getRequestDispatcher("normaluser.jsp").forward(request,response);
    }
    //增加理发师
    public void barberAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String packages = request.getParameter("packages");
        int price = Integer.parseInt(request.getParameter("price"));
        String time = request.getParameter("time");
        Barber barber = new Barber(name,packages,price,time);
        System.out.println(barber);
        int ret = service.addBarber(barber);
        response.sendRedirect("barber?m=list&page=1");
    }
    //删除理发师
    public void barberDelete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        int i = service.deleteBarber(id);
        if(i > 0) {
            request.getRequestDispatcher("barber?m=list&page=1").forward(request,response);
        }
    }
    //修改理发师信息
    public void barberUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String packages = request.getParameter("packages");
        int price = Integer.parseInt(request.getParameter("price"));
        String time = request.getParameter("time");
        Barber barber = new Barber(id,name,packages,price,time);
        int BarberAdminServletret = service.updateBarber(barber);
        if(ret > 0) {
            response.sendRedirect("barber?m=list&page=1");
        }
    }
    //展示理发师信息页面
    public void barberUpdateShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Barber barber = service.findById(id);
        request.setAttribute("barber", barber);
        request.getRequestDispatcher("barberUpdate.jsp").forward(request, response);
    }
    //删除用户
    public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        int i = service.delete(id);
        if(i > 0) {
            request.getRequestDispatcher("/admin/barber?m=manageAdminServlet").forward(request,response);
        }
    }
    //根据名字起点查询理发师
    public void selectByName(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        //查询到的车票列表
        List<Barber> list = service.findBarberByName(name);
        request.setAttribute("BarberList1",list);
        //列表发送到前端
        request.getRequestDispatcher("/admin/barber?m=list&page=1").forward(request,response);
    }
    //分页查询
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int page = 1;
        String currentPage = request.getParameter("page");
        if(currentPage != null && currentPage != null) {
            page = Integer.parseInt(currentPage);
        }
        PageDto<Barber> pageDto = service.listBarber(page,PageSize);
        request.setAttribute("pageDto",pageDto);
        request.getRequestDispatcher("manage.jsp").forward(request,response);
    }

}
